<?php 
namespace app\dboard\logic;
use think\Db;
use think\facade\Session;
class Students extends Base
{

    function __construct()
    {
    }

    public function create($param)
    {
        $sid = isset($param['sid']) ? intval($param['sid']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        $age = isset($param['age']) ? trim($param['age']):0;
        $father_name = isset($param['father_name']) ? trim($param['father_name']):'';
        $mother_name = isset($param['mother_name']) ? trim($param['mother_name']):'';
        $phone = isset($param['phone']) ? trim($param['phone']):'';
        $study_status = isset($param['study_status']) ? trim($param['study_status']):'';
        $remark = isset($param['remark']) ? trim($param['remark']):'';
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        if (!$name) {
          $this->res_data['Msg'] = "请填写学员姓名";
          return $this->res_data;
        }
        if (!$age) {
          $this->res_data['Msg'] = "请填写年龄";
          return $this->res_data;
        }
        if (!$father_name) {
          $this->res_data['Msg'] = "请填写爸爸姓名";
          return $this->res_data;
        }
        if (!$mother_name) {
          $this->res_data['Msg'] = "请填写妈妈姓名";
          return $this->res_data;
        }
        if (!$phone) {
          $this->res_data['Msg'] = "请填写联系电话";
          return $this->res_data;
        }
        if (Db::name('user')->where(['phone'=>$phone,'sid'=>$sid,'status'=>1])->find()) {
          $this->res_data['Msg'] = "该联系电话已注册，请使用其他号码";
          return $this->res_data;
        }
        if (!$study_status) {
          $this->res_data['Msg'] = "请选择排课状态";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
            $this->res_data['Msg'] = "无此门店";
            return $this->res_data;
        }
        $data['sid'] =$sid;
        $data['name'] =$name;
        $data['age'] =$age;
        $data['father_name'] =$father_name;
        $data['mother_name'] =$mother_name;
        $data['phone'] =$phone;
        $data['study_status'] =$study_status;
        $data['remark'] =$remark;
        $data['status'] =1;
        $data['create_time'] =time();
        $data['update_time'] =time();
        $cdata = Db::name('user')->insertGetId($data);
        if(!$cdata) {
            $this->res_data['Msg'] = '添加学员失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }
    public function edit($param)
    {
        $cid = isset($param['cid']) ? intval($param['cid']):0;
        $sid = isset($param['sid']) ? intval($param['sid']):0;
        $name = isset($param['name']) ? trim($param['name']):'';
        $age = isset($param['age']) ? trim($param['age']):0;
        $father_name = isset($param['father_name']) ? trim($param['father_name']):'';
        $mother_name = isset($param['mother_name']) ? trim($param['mother_name']):'';
        $phone = isset($param['phone']) ? trim($param['phone']):'';
        $study_status = isset($param['study_status']) ? trim($param['study_status']):'';
        $remark = isset($param['remark']) ? trim($param['remark']):'';
        $status = isset($param['status']) ? trim($param['status']):'';
        if (!$sid || !$cid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        if (!$name) {
          $this->res_data['Msg'] = "请填写学员姓名";
          return $this->res_data;
        }
        if (!$age) {
          $this->res_data['Msg'] = "请填写年龄";
          return $this->res_data;
        }
        if (!$father_name) {
          $this->res_data['Msg'] = "请填写爸爸姓名";
          return $this->res_data;
        }
        if (!$mother_name) {
          $this->res_data['Msg'] = "请填写妈妈姓名";
          return $this->res_data;
        }
        if (!$phone) {
          $this->res_data['Msg'] = "请填写联系电话";
          return $this->res_data;
        }
        $user= Db::name('user')->where([['id','=',$cid]])->find();
        if (!$user) {
            $this->res_data['Msg'] = "无此学员";
            return $this->res_data;
        }
        if ($phone!=$user['phone'] && Db::name('user')->where(['phone'=>$phone,'sid'=>$sid,'status'=>1])->find()) {
          $this->res_data['Msg'] = "该联系电话已注册，请使用其他号码";
          return $this->res_data;
        }
        if (!$study_status) {
          $this->res_data['Msg'] = "请选择排课状态";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
            $this->res_data['Msg'] = "无此门店";
            return $this->res_data;
        }
        $data['name'] =$name;
        $data['age'] =$age;
        $data['father_name'] =$father_name;
        $data['mother_name'] =$mother_name;
        $data['phone'] =$phone;
        $data['study_status'] =$study_status;
        $data['remark'] =$remark;
        // $data['status'] =$status;
        $data['create_time'] =time();
        $data['update_time'] =time();
        Db::startTrans();
        try {
            $cdata = Db::name('user')->where([['id','=',$cid]])->data($data)->update();
            if(!$cdata) {
                Db::rollback();
                $this->res_data['Msg'] = '编辑失败';
                return $this->res_data;
            }
            Db::commit();
            $this->res_data['Msg'] = '成功';
            $this->res_data['Code'] = 0;
            return $this->printResult($this->res_data);
        } catch (\Exception $e) {
            Db::rollback();
            $this->res_data['Msg'] = '操作失败';
            return $this->res_data;
        }
    }
    public function delete($param)
    {
        $cid = isset($param['cid']) ? intval($param['cid']):0;
        $status = isset($param['status']) ? intval($param['status']):0;
        $user = Db::name('user')->where([['id','=',$cid]])->find();
        if (!$user) {
            $this->res_data['Msg'] = "无此班级";
            return $this->res_data;
        }
        $data['status'] =$status;
        $data['update_time'] =time();
        $cdata = Db::name('user')->where([['id','=',$cid]])->data($data)->update();
        if(!$cdata) {
            $this->res_data['Msg'] = '更新失败';
            return $this->res_data;
        }
        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->printResult($this->res_data);
    }



    public function ajaxList($sid,$study_status,$study_status_arr,$page_start=0,$page_limit=0)
    {
        $where = [];
        $where[] = ['sid','=',$sid];
        $where[] = ['status','=',1];
        $where[] = ['is_teacher','=',0];
        if ($study_status) {
            $where[] = ['study_status','=',$study_status];
        }
        $field = '*';
        $count=Db::name('user')->where($where)->count();
        if ($page_limit) {
            $list  = Db::name('user')
                  ->field($field)
                  ->where($where)
                  ->order('id desc')
                  ->limit($page_start, $page_limit)->select();
        } else {
            $list  = Db::name('user')
                  ->field($field)
                  ->where($where)
                  ->order('id desc')->select();
        }
        foreach($list as $k=>&$v){
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            $v['course'] = '';
            $v['times'] = 0;
            if ($v['study_status']>0) {
                $v['study_status'] = $study_status_arr[$v['study_status']];
            } else {
                $v['study_status'] = '';
            }
            $course = Db::name('user_course')->alias('uc')->leftJoin('course c','c.id=uc.course_id')->field('c.id,c.name,c.periods,uc.classtime_surplus,uc.classtime_sum')->where([['uc.user_id','=',$v['id']],['uc.status','=',2]])->select();
            if ($course) {
                foreach ($course as $key => $value) {
                    $v['course'] = $value['name'].',';
                    $v['times']+=$value['classtime_surplus'];
                }
            }
        }
        return [$list,$count];
    }
}

?>
